package ynwx.libaray.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.text.TextUtils;

/**
 * Created by Chron on 2017/11/20.
 */

public class Utils {
    private static final int PHONE_NUM_LENGTH = 11;

    public static boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    /**
     * 判断当前字符串是否为手机号格式
     * http://blog.csdn.net/bitian123/article/details/51506961
     *
     * @param phoneNum
     * @return
     */
    public static boolean isMobilePhoneNum(String phoneNum) {
        String num = "[1][35789]\\d{9}";
        String phone = phoneNum.trim();
        if (TextUtils.isEmpty(phoneNum) || phone.length() != PHONE_NUM_LENGTH) {
            return false;
        } else {
            return phoneNum.matches(num);
        }
    }

    /**
     *  点击跳转拨号页面
     * @param context
     * @param phoneNumber
     */
    public static void toPhoneCall(Context context,String phoneNumber){
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }


}
